﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using MySql.Data.MySqlClient;
using NLog.Extensions.Logging;
using Serilog;

namespace doris__dotnet_console_test
{
    public class DorisSqlHelper
    {
        public static string ConnectionString { get; set; }
        public static ILogger<DorisSqlHelper> logger = LoggerFactory.Create(builder => builder.AddNLog()).CreateLogger<DorisSqlHelper>();


        /// <summary>
        /// 
        /// </summary>
        /// <param name="strSql"></param>
        /// <returns></returns>
        public static DataTable ExecuteDt(string strSql)
        {
            MySqlConnection mySqlConnection = null;
            MySqlDataAdapter da = null;
            DataSet ds = null;
            try
            {
                using (mySqlConnection = new MySqlConnection(ConnectionString))
                {
                    da = new MySqlDataAdapter(strSql, mySqlConnection);
                    ds = new DataSet();
                    mySqlConnection.Open();
                    da.Fill(ds);//in 查询 耗时30秒以上
                    mySqlConnection.Close();
                    return ds.Tables[0];
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
                Log.Error(strSql);
            }
            finally
            {
                if (mySqlConnection != null && mySqlConnection.State == ConnectionState.Open)
                    mySqlConnection.Close();
                if (da != null)
                    da.Dispose();
                if (ds != null)
                    ds.Dispose();
            }
            return null;
        }
    }
}
